﻿namespace Chain.IDAL
{
    using Chain.DBUtility;
    using Chain.Model;
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Text;

    public class SysCustomRemind
    {
        public int Add(Chain.Model.SysCustomRemind model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into SysCustomRemind(");
            builder.Append("CustomRemindTitle,CustomRemindDetail,CustomReminder,CustomRemindTime,CustomRemindCreateTime,CustomRemindShopID,CustomRemindUserID)");
            builder.Append(" values (");
            builder.Append("@CustomRemindTitle,@CustomRemindDetail,@CustomReminder,@CustomRemindTime,@CustomRemindCreateTime,@CustomRemindShopID,@CustomRemindUserID)");
            builder.Append(";select @@IDENTITY");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@CustomRemindTitle", SqlDbType.NVarChar, 300), new SqlParameter("@CustomRemindDetail", SqlDbType.NVarChar, 500), new SqlParameter("@CustomReminder", SqlDbType.VarChar, 50), new SqlParameter("@CustomRemindTime", SqlDbType.DateTime), new SqlParameter("@CustomRemindCreateTime", SqlDbType.DateTime), new SqlParameter("@CustomRemindShopID", SqlDbType.Int, 4), new SqlParameter("@CustomRemindUserID", SqlDbType.Int, 4) };
            cmdParms[0].Value = model.CustomRemindTitle;
            cmdParms[1].Value = model.CustomRemindDetail;
            cmdParms[2].Value = model.CustomReminder;
            cmdParms[3].Value = model.CustomRemindTime;
            cmdParms[4].Value = model.CustomRemindCreateTime;
            cmdParms[5].Value = model.CustomRemindShopID;
            cmdParms[6].Value = model.CustomRemindUserID;
            object single = DbHelperSQL.GetSingle(builder.ToString(), cmdParms);
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public bool Delete(int CustomRemindID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from SysCustomRemind ");
            builder.Append(" where CustomRemindID=@CustomRemindID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@CustomRemindID", SqlDbType.Int, 4) };
            cmdParms[0].Value = CustomRemindID;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }

        public bool DeleteList(string CustomRemindIDlist)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from SysCustomRemind ");
            builder.Append(" where CustomRemindID in (" + CustomRemindIDlist + ")  ");
            return (DbHelperSQL.ExecuteSql(builder.ToString()) > 0);
        }

        public bool Exists(int CustomRemindID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) from SysCustomRemind");
            builder.Append(" where CustomRemindID=@CustomRemindID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@CustomRemindID", SqlDbType.Int, 4) };
            cmdParms[0].Value = CustomRemindID;
            return DbHelperSQL.Exists(builder.ToString(), cmdParms);
        }

        public DataSet GetCustomField()
        {
            string str = "select * from SysCustomRemind where CustomRemindTime = convert(varchar(10),getdate(),120)";
            return DbHelperSQL.Query(str.ToString());
        }

        public DataSet GetList(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select SysCustomRemind.* ,(select ShopName from SysShop where SysCustomRemind.CustomRemindShopID = SysShop.ShopID) as ShopName,(select UserName from SysUser where SysCustomRemind.CustomRemindUserID = SysUser.UserID) as UserName ");
            builder.Append(" FROM SysCustomRemind ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" order by CustomRemindTime asc ");
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetList(string strWhere, int count)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select top " + count + " SysCustomRemind.* ,(select ShopName from SysShop where SysCustomRemind.CustomRemindShopID = SysShop.ShopID) as ShopName,(select UserName from SysUser where SysCustomRemind.CustomRemindUserID = SysUser.UserID) as UserName ");
            builder.Append(" FROM SysCustomRemind ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" order by CustomRemindTime asc ");
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select ");
            if (Top > 0)
            {
                builder.Append(" top " + Top.ToString());
            }
            builder.Append(" CustomRemindID,CustomRemindTitle,CustomRemindDetail,CustomReminder,CustomRemindTime,CustomRemindCreateTime,CustomRemindShopID,CustomRemindUserID ");
            builder.Append(" FROM SysCustomRemind ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SELECT * FROM ( ");
            builder.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                builder.Append("order by T." + orderby);
            }
            else
            {
                builder.Append("order by T.CustomRemindID desc");
            }
            builder.Append(")AS Row, T.*  from SysCustomRemind T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                builder.Append(" WHERE " + strWhere);
            }
            builder.Append(" ) TT");
            builder.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(builder.ToString());
        }

        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("CustomRemindID", "SysCustomRemind");
        }

        public Chain.Model.SysCustomRemind GetModel(int CustomRemindID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select  top 1 CustomRemindID,CustomRemindTitle,CustomRemindDetail,CustomReminder,CustomRemindTime,CustomRemindCreateTime,CustomRemindShopID,CustomRemindUserID from SysCustomRemind ");
            builder.Append(" where CustomRemindID=@CustomRemindID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@CustomRemindID", SqlDbType.Int, 4) };
            cmdParms[0].Value = CustomRemindID;
            Chain.Model.SysCustomRemind remind = new Chain.Model.SysCustomRemind();
            DataSet set = DbHelperSQL.Query(builder.ToString(), cmdParms);
            if (set.Tables[0].Rows.Count <= 0)
            {
                return null;
            }
            if ((set.Tables[0].Rows[0]["CustomRemindID"] != null) && (set.Tables[0].Rows[0]["CustomRemindID"].ToString() != ""))
            {
                remind.CustomRemindID = int.Parse(set.Tables[0].Rows[0]["CustomRemindID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["CustomRemindTitle"] != null) && (set.Tables[0].Rows[0]["CustomRemindTitle"].ToString() != ""))
            {
                remind.CustomRemindTitle = set.Tables[0].Rows[0]["CustomRemindTitle"].ToString();
            }
            if ((set.Tables[0].Rows[0]["CustomRemindDetail"] != null) && (set.Tables[0].Rows[0]["CustomRemindDetail"].ToString() != ""))
            {
                remind.CustomRemindDetail = set.Tables[0].Rows[0]["CustomRemindDetail"].ToString();
            }
            if ((set.Tables[0].Rows[0]["CustomReminder"] != null) && (set.Tables[0].Rows[0]["CustomReminder"].ToString() != ""))
            {
                remind.CustomReminder = set.Tables[0].Rows[0]["CustomReminder"].ToString();
            }
            if ((set.Tables[0].Rows[0]["CustomRemindTime"] != null) && (set.Tables[0].Rows[0]["CustomRemindTime"].ToString() != ""))
            {
                remind.CustomRemindTime = DateTime.Parse(set.Tables[0].Rows[0]["CustomRemindTime"].ToString());
            }
            if ((set.Tables[0].Rows[0]["CustomRemindCreateTime"] != null) && (set.Tables[0].Rows[0]["CustomRemindCreateTime"].ToString() != ""))
            {
                remind.CustomRemindCreateTime = DateTime.Parse(set.Tables[0].Rows[0]["CustomRemindCreateTime"].ToString());
            }
            if ((set.Tables[0].Rows[0]["CustomRemindShopID"] != null) && (set.Tables[0].Rows[0]["CustomRemindShopID"].ToString() != ""))
            {
                remind.CustomRemindShopID = int.Parse(set.Tables[0].Rows[0]["CustomRemindShopID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["CustomRemindUserID"] != null) && (set.Tables[0].Rows[0]["CustomRemindUserID"].ToString() != ""))
            {
                remind.CustomRemindUserID = int.Parse(set.Tables[0].Rows[0]["CustomRemindUserID"].ToString());
            }
            return remind;
        }

        public int GetRecordCount(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) FROM SysCustomRemind ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            object single = DbHelperSQL.GetSingle(builder.ToString());
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public bool Update(Chain.Model.SysCustomRemind model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update SysCustomRemind set ");
            builder.Append("CustomRemindTitle=@CustomRemindTitle,");
            builder.Append("CustomRemindDetail=@CustomRemindDetail,");
            builder.Append("CustomReminder=@CustomReminder,");
            builder.Append("CustomRemindTime=@CustomRemindTime,");
            builder.Append("CustomRemindCreateTime=@CustomRemindCreateTime,");
            builder.Append("CustomRemindShopID=@CustomRemindShopID,");
            builder.Append("CustomRemindUserID=@CustomRemindUserID");
            builder.Append(" where CustomRemindID=@CustomRemindID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@CustomRemindTitle", SqlDbType.NVarChar, 300), new SqlParameter("@CustomRemindDetail", SqlDbType.NVarChar, 500), new SqlParameter("@CustomReminder", SqlDbType.VarChar, 50), new SqlParameter("@CustomRemindTime", SqlDbType.DateTime), new SqlParameter("@CustomRemindCreateTime", SqlDbType.DateTime), new SqlParameter("@CustomRemindShopID", SqlDbType.Int, 4), new SqlParameter("@CustomRemindUserID", SqlDbType.Int, 4), new SqlParameter("@CustomRemindID", SqlDbType.Int, 4) };
            cmdParms[0].Value = model.CustomRemindTitle;
            cmdParms[1].Value = model.CustomRemindDetail;
            cmdParms[2].Value = model.CustomReminder;
            cmdParms[3].Value = model.CustomRemindTime;
            cmdParms[4].Value = model.CustomRemindCreateTime;
            cmdParms[5].Value = model.CustomRemindShopID;
            cmdParms[6].Value = model.CustomRemindUserID;
            cmdParms[7].Value = model.CustomRemindID;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }
    }
}

